package com.tensquare.test.serarch.controller;

import com.tensquare.test.serarch.pojo.Article;
import com.tensquare.test.serarch.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService ;

    @RequestMapping(value="/{key}/{page}/{size}",method=RequestMethod.GET)
    public Result findBykey(@PathVariable String key,@PathVariable int page,@PathVariable int size){
        Page<Article> pageData=articleService.findByKey(key,page,size);
        PageResult<Article> articlePageResult = new PageResult<>(pageData.getTotalElements(), pageData.getContent());
        return new Result(true, StatusCode.OK,"查找成功",articlePageResult);

    }

    @RequestMapping(method=RequestMethod.POST)
    public Result save(@RequestBody Article article){

        articleService.save(article);
        return new Result(true, StatusCode.OK,"添加成功");

    }
}
